package com.etime.yqtsponsor.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.yqtsponsor.dto.VoteRegisterDto;
import com.etime.yqtsponsor.dto.VoteRegisterDtos;
import com.etime.yqtsponsor.pojo.VoteRegister;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: LouisLew
 * @date: 2022/07/19/16:54
 * @description: 参与活动用户的数据传输层
 */
@Repository
public interface VoteRegisterMapper extends BaseMapper<VoteRegister> {

    @Select("SELECT" +
            "  *" +
            " FROM" +
            "(SELECT t1.*," +
            " pic.showPic" +
            " FROM" +
            "(SELECT vote_register.*, " +
            "        users.nickname" +
            "        FROM" +
            "        vote_register" +
            "        LEFT JOIN" +
            "        users" +
            "        ON" +
            "        vote_register.userId = users.id) AS t1" +
            " LEFT JOIN" +
            " active_pic_bank pic" +
            " ON" +
            " t1.showPicId = pic.id) AS t2 ${ew.customSqlSegment}")
    Page<VoteRegisterDto> selectRegisterByPage(Page<VoteRegisterDto> page, @Param(Constants.WRAPPER) QueryWrapper<VoteRegisterDto> wrapper);

    @Select("SELECT" +
            "  *" +
            " FROM" +
            "(SELECT t1.*," +
            " pic.showPic" +
            " FROM" +
            "(SELECT vote_register.*, " +
            "        users.nickname" +
            "        FROM" +
            "        vote_register" +
            "        LEFT JOIN" +
            "        users" +
            "        ON" +
            "        vote_register.userId = users.id) AS t1" +
            " LEFT JOIN" +
            " active_pic_bank pic" +
            " ON" +
            " t1.showPicId = pic.id) AS t2 ${ew.customSqlSegment}")
    List<VoteRegisterDto> selectVoteRegisterList(@Param(Constants.WRAPPER) QueryWrapper<VoteRegisterDto> wrapper);

    @Select("SELECT" +
            "  *" +
            " FROM" +
            " (SELECT" +
            " vote_register.*, " +
            " survey_answer.answers" +
            " FROM" +
            " vote_register" +
            " LEFT JOIN" +
            " survey_answer" +
            " ON " +
            " vote_register.userId = survey_answer.userId" +
            " AND vote_register.activeId = survey_answer.activeId) AS t1 ${ew.customSqlSegment}")
    List<VoteRegisterDtos> selectSurveyFormRegisterList(@Param(Constants.WRAPPER) QueryWrapper<VoteRegisterDtos> wrapper);

    @Select("SELECT" +
            "  *" +
            " FROM" +
            " (SELECT" +
            " vote_register.*," +
            " active_group.`name`, " +
            " active_pic_bank.showPic" +
            " FROM" +
            " vote_register" +
            " LEFT JOIN" +
            " active_group" +
            " ON " +
            "  vote_register.groupId = active_group.id" +
            " LEFT JOIN" +
            " active_pic_bank" +
            " ON " +
            "  vote_register.showPicId = active_pic_bank.id) AS t1 ${ew.customSqlSegment}")
    List<VoteRegisterDto> selectExportVote(@Param(Constants.WRAPPER) QueryWrapper<VoteRegisterDto> wrapper);

    @Select("SELECT" +
            "  *" +
            " FROM" +
            " (SELECT" +
            " vote_register.activeId, " +
            " vote_register.id," +
            " vote_register.title, " +
            " vote_register.userId, " +
            " vote_register.createdTime, " +
            " vote_register.formContent, " +
            " vote_register.`status`, " +
            " survey_answer.answers" +
            " FROM" +
            " vote_register" +
            " INNER JOIN" +
            " survey_answer" +
            " ON " +
            "  vote_register.activeId = survey_answer.activeId AND" +
            "  vote_register.userId = survey_answer.userId) AS t1 ${ew.customSqlSegment}")
    List<VoteRegisterDtos> selectExportForm(@Param(Constants.WRAPPER) QueryWrapper<VoteRegisterDtos> wrapper);
}
